﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;

using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid;
using DevExpress.Utils;

using DevExpress.XtraEditors.Repository;

using MM.Entities;
using MM.Business; 

namespace MM
{
    public partial class frmChinhsuaPhieuXK : Form
    {
        
        string _matau = "";
        string _nguoitao = "";
        string _sConn = "";
        
        List<CHI_TIET_PXH> lstTonKho = new List<CHI_TIET_PXH>();
        PHIEU_XUAT_HANG objPXH;

        frmTimKiemVTPT frm;
        frmTimKiemTheoHinh frm1;
        frmImpa_TimKiem frm2;
        frmImpa_TimKiemTheoHinh frm3;
        public List<VAT_TU_PHU_TUNG> lstVTPT = new List<VAT_TU_PHU_TUNG>();

        public frmChinhsuaPhieuXK(string sConn)
        {
            InitializeComponent();
            _sConn = sConn;
            _matau = frmDangNhap.objTau._tenTau2KiTu;
            _nguoitao = frmDangNhap.objTaiKhoan._soCMND;
           

            this.HienthiPHIEU_XUAT_HANG_MAX(_matau);
            this.HienthiCHI_TIET_PXH(this.txtMaPhieuXuatHang.Text);
        }

        private void HienthiPHIEU_XUAT_HANG_MAX(string matau)
        {
            try
            {
                clsPHIEU_XUAT_HANG pxhCtrl = new clsPHIEU_XUAT_HANG(_sConn);
                List<PHIEU_XUAT_HANG> lst = new List<PHIEU_XUAT_HANG>();
                lst = pxhCtrl.usp_SelectPHIEU_XUAT_HANG_MAX(matau);
                if (lst.Count > 0)
                {
                    objPXH = new PHIEU_XUAT_HANG();
                    objPXH = lst[0];
                    this.txtMaPhieuXuatHang.Text = lst[0].MaPhieuXuatHang;
                    this.txtNgayThucHien.Text = lst[0].NgayXuat.Day.ToString() + "/" + lst[0].NgayXuat.Month.ToString() + "/" + lst[0].NgayXuat.Year.ToString();
                    this.txtNguoiThucHien.Text = lst[0].NguoiXuat;

                  
                    if (lst[0].NgayExport.HasValue || lst[0].TinhTrang == 1)
                    {                       
                        if (lst[0].TinhTrang != 1)
                        {
                            DateTime? dE = lst[0].NgayExport;
                            this.txtNgayDuyet.Text = dE.Value.Day.ToString() + "/" + dE.Value.Month.ToString() + "/" + dE.Value.Year.ToString();
                            this.chkXoa.Checked = false;
                        }
                        else
                            this.chkXoa.Checked = true;
                        this.cmdThemVTPT.Enabled = false;
                        this.cmdCapNhatSL.Enabled = false;
                        this.gridColumn5.Visible = false;// cot Xoa
                    }
                    else
                    {
                        this.txtNgayDuyet.Text = "";
                        this.cmdThemVTPT.Enabled = true;
                        this.cmdCapNhatSL.Enabled = true;
                        this.gridColumn5.Visible = true;// cot Xoa
                    }



                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void HienthiPHIEU_XUAT_HANG(string MaPhieuXuatHang)
        {
            clsPHIEU_XUAT_HANG pxhCtrl = new clsPHIEU_XUAT_HANG(_sConn);
            List<PHIEU_XUAT_HANG> lst = new List<PHIEU_XUAT_HANG>();
            lst = pxhCtrl.usp_SelectPHIEU_XUAT_HANG(MaPhieuXuatHang);
            if (lst.Count > 0)
            {
                this.txtMaPhieuXuatHang.Text = MaPhieuXuatHang;
                objPXH = new PHIEU_XUAT_HANG();
                objPXH = lst[0];

                this.txtNgayThucHien.Text = lst[0].NgayXuat.Day.ToString() + "/" + lst[0].NgayXuat.Month.ToString() + "/" + lst[0].NgayXuat.Year.ToString();
                this.txtNguoiThucHien.Text = lst[0].NguoiXuat;
                //if (lst[0].NgayExport.Year > 2011)
                if (lst[0].NgayExport.HasValue || lst[0].TinhTrang == 1)
                {
                    if (lst[0].TinhTrang != 1)
                    {
                        DateTime? dE = lst[0].NgayExport;
                        this.txtNgayDuyet.Text = dE.Value.Day.ToString() + "/" + dE.Value.Month.ToString() + "/" + dE.Value.Year.ToString();
                        this.chkXoa.Checked = false;
                    }
                    else
                        this.chkXoa.Checked = true;
                    this.cmdThemVTPT.Enabled = false;
                    this.cmdCapNhatSL.Enabled = false;
                    this.gridColumn5.Visible = false;// cot Xoa
                }
                else
                {
                    this.txtNgayDuyet.Text = "";
                    this.cmdThemVTPT.Enabled = true;
                    this.cmdCapNhatSL.Enabled = true;
                    this.gridColumn5.Visible = true;// cot Xoa
                }

                
            }

        }

        private void HienthiCHI_TIET_PXH(string MaPhieuXuatHang)
        {
            lstTonKho.Clear();
            clsCHI_TIET_PXH ctpxhCtrl = new clsCHI_TIET_PXH(_sConn);
            
            lstTonKho = ctpxhCtrl.usp_SelectCHI_TIET_PXH_ByMaPhieuXuatHang(this.txtMaPhieuXuatHang.Text.Trim());
            lstTonKho.RemoveAll(delegate(CHI_TIET_PXH x) { return x.TinhTrang == 1; });
            this.gridControl1.DataSource = lstTonKho;
            this.gridControl1.Refresh();
            gridDSVT.ClearSelection();
            gridDSVT.Tag = 0;
        }

        private void txtMaPhieuXuatHang_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (this.txtMaPhieuXuatHang.Text.Trim().Length == 10)
                {
                    this.HienthiCHI_TIET_PXH(this.txtMaPhieuXuatHang.Text.Trim());
                    this.HienthiPHIEU_XUAT_HANG(this.txtMaPhieuXuatHang.Text.Trim());

                  
                }
            }
        }

        private void InitDMKho(string mavtpt)
        {

            clsKHO khoCtrl = new clsKHO(_sConn);
            List<KHO> lst = new List<KHO>();
            lst = khoCtrl.usp_SelectKHO_ByMaVTPT_TON_KHO(mavtpt);
            this.lookUpKho.Properties.DataSource = lst;
            this.lookUpKho.Properties.DisplayMember = "TenKho";
            this.lookUpKho.Properties.ValueMember = "MaKho";
        }
        private void InitDMTrangThai(string mavtpt, string makho)
        {
            clsTRANG_THAI_VTPT ttCtrl = new clsTRANG_THAI_VTPT(_sConn);
            List<TRANG_THAI_VTPT> lst = new List<TRANG_THAI_VTPT>();
            lst = ttCtrl.usp_SelectTRANG_THAI_VTPTByMaVTPT_TON_KHO(mavtpt, makho);
            this.lookUpTrangthai.Properties.DataSource = lst;
            this.lookUpTrangthai.Properties.DisplayMember = "TenTrangThai";
            this.lookUpTrangthai.Properties.ValueMember = "MaTrangThai";
            if (lst.Count > 0)
            {
                this.lookUpTrangthai.Properties.ForceInitialize();
                this.lookUpTrangthai.ItemIndex = 0;
            }
        }
        private void InitKho(string mavtpt)
        {
            clsKHO khoCtrl = new clsKHO(_sConn);
            List<KHO> lst = new List<KHO>();
            lst = khoCtrl.usp_SelectKHO_ByMaVTPT_TON_KHO(mavtpt);
            this.lookUpKho.Properties.DataSource = lst;
            this.lookUpKho.Properties.DisplayMember = "TenKho";
            this.lookUpKho.Properties.ValueMember = "MaKho";
        }
        private void InitTrangthai(string mavtpt, string makho)
        {
            clsTRANG_THAI_VTPT ttCtrl = new clsTRANG_THAI_VTPT(_sConn);
            List<TRANG_THAI_VTPT> lst = new List<TRANG_THAI_VTPT>();
            lst = ttCtrl.usp_SelectTRANG_THAI_VTPTByMaVTPT_TON_KHO(mavtpt, makho);
            this.lookUpTrangthai.Properties.DataSource = lst;
            this.lookUpTrangthai.Properties.DisplayMember = "TenTrangThai";
            this.lookUpTrangthai.Properties.ValueMember = "MaTrangThai";
            if (lst.Count > 0)
            {
                this.lookUpTrangthai.Properties.ForceInitialize();
                this.lookUpTrangthai.ItemIndex = 0;
            }
        }

        private void gridDSVT_Click(object sender, EventArgs e)
        {
            GridView view = (GridView)sender;
            GridHitInfo info = view.CalcHitInfo(gridControl1.PointToClient(MousePosition));

            if (info.InRow || info.InRowCell)
            {
                if (this.txtNgayDuyet.Text != "")// da duyet
                {
                    return;
                }

                if (lstTonKho[info.RowHandle].TinhTrang != 0)
                {
                    this.cmdThemVTPT.Enabled = false;
                    this.cmdCapNhatSL.Enabled = false;
                    return;
                }
                else
                {
                    this.cmdThemVTPT.Enabled = true;
                    this.cmdCapNhatSL.Enabled = true;
                }

                float soluongmax = 0;

                this.txtMaVTPT.Text = lstTonKho[info.RowHandle].MaThietBi;
                this.lblTenVTPT.Text = lstTonKho[info.RowHandle].TenVTPT;
                this.txtSoLuong.Text = lstTonKho[info.RowHandle].SoLuong.ToString();

                this.InitKho(this.txtMaVTPT.Text);
                this.InitTrangthai(this.txtMaVTPT.Text, lstTonKho[info.RowHandle].MaKho);

                this.lookUpKho.ItemIndex = this.lookUpKho.Properties.GetDataSourceRowIndex("MaKho", lstTonKho[info.RowHandle].MaKho);
                this.lookUpTrangthai.ItemIndex = this.lookUpTrangthai.Properties.GetDataSourceRowIndex("MaTrangThai", lstTonKho[info.RowHandle].MaTrangThai);


                clsTON_KHO tonCtrl = new clsTON_KHO(_sConn);
                List<TON_KHO> lstton = new List<TON_KHO>();
                lstton = tonCtrl.usp_SelectTON_KHO_ByMaKho(lstTonKho[info.RowHandle].MaKho, lstTonKho[info.RowHandle].MaThietBi, lstTonKho[info.RowHandle].MaTrangThai, null, null);
                for (int i = 0; i < lstton.Count; i++)
                {
                    soluongmax += lstton[i].SoLuong;
                }

                // kiem tra xem VTPT nay co cac phieu xuat kho nao chua duoc duyet
                List<CHI_TIET_PXH> lstctpxh = new List<CHI_TIET_PXH>();
                clsCHI_TIET_PXH ctpxhCtrl = new clsCHI_TIET_PXH(_sConn);
                lstctpxh = ctpxhCtrl.usp_CheckSoLuongXuatKhoChuaDuyet(lstTonKho[info.RowHandle].MaThietBi, lstTonKho[info.RowHandle].MaKho, lstTonKho[info.RowHandle].MaTrangThai);
                for (int i = 0; i < lstctpxh.Count; i++)
                {
                    if (lstctpxh[i].MaPhieuXuatHang!=this.txtMaPhieuXuatHang.Text)
                        soluongmax -= lstctpxh[i].SoLuong;
                }

                if (lstctpxh.Count > 0) // neu ton tai danh sach phieu xuat kho chua duyet thi hien thi link xem danh sach
                {
                    if (lstctpxh.Count==1 && lstctpxh[0].MaPhieuXuatHang==this.txtMaPhieuXuatHang.Text)
                        this.lnkPhieuChuaDuyet.Visible = false;
                    else
                        this.lnkPhieuChuaDuyet.Visible = true;
                }
                else
                    this.lnkPhieuChuaDuyet.Visible = false;

                // kiem tra xem VTPT nay da ton tai trong list can xuat kho chua
                for (int i = 0; i < lstTonKho.Count; i++)
                {
                    if (lstTonKho[i].MaThietBi == lstTonKho[info.RowHandle].MaThietBi && lstTonKho[i].MaKho == lstTonKho[info.RowHandle].MaKho && lstTonKho[i].MaTrangThai == lstTonKho[info.RowHandle].MaTrangThai)
                        soluongmax -= lstTonKho[i].SoLuong;
                }
                soluongmax += lstTonKho[info.RowHandle].SoLuong;

                this.lblsoluongton.Text = soluongmax.ToString();

                if (lstTonKho[info.RowHandle].POThamChieuSAP != "")// xuat kho gia cong hoac xuat kho do chuyen kho khac plant
                {
                    this.lookUpTrangthai.Enabled = false;
                }
                else
                {
                    this.lookUpTrangthai.Enabled = true;
                }

            }
        }     
        

        

        private void label1_Click(object sender, EventArgs e)
        {
            CHI_TIET_PXH obj = new CHI_TIET_PXH();
            
            frmTimPhieuXK f = new frmTimPhieuXK(_sConn,obj,0);
            f.chonPXK += new frmTimPhieuXK.ChonPhieuXuatKho(GetPhieuXuatKho);
            f.Show(this);
        }

        private void GetPhieuXuatKho(object sender, ChonPhieuXKEventArgs e)
        {
            string mapxk = e.MaPhieuXuatKho;

            this.HienthiPHIEU_XUAT_HANG(mapxk);
            this.HienthiCHI_TIET_PXH(mapxk);            
        }

        private void frmChinhsuaPhieuXK_Load(object sender, EventArgs e)
        {
            Common.SetColor(this);
        }

        private void lnkXoa_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.txtNgayDuyet.Text == "")
                {
                    if (MessageBox.Show("Bạn có thật sự muốn xóa?", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        int MaChiTiet = int.Parse(gridDSVT.GetRowCellValue(gridDSVT.FocusedRowHandle, "MaChiTiet").ToString());
                        if (MaChiTiet > 0) // danh dau xoa trong CSDL
                        {
                            CHI_TIET_PXH obj = new CHI_TIET_PXH();
                            obj = lstTonKho[gridDSVT.FocusedRowHandle];
                            obj.TinhTrang = 1;
                            obj.NgayNhap = DateTime.Today;
                            obj.NguoiNhap = _nguoitao;

                            clsCHI_TIET_PXH ctpxhCtrl = new clsCHI_TIET_PXH(_sConn);
                            if (ctpxhCtrl.usp_UpdateCHI_TIET_PXH(obj) > 0)
                            {
                                lstTonKho.RemoveAt(gridDSVT.FocusedRowHandle);
                                this.gridControl1.RefreshDataSource();

                                if (lstTonKho.Count == 0)
                                {
                                    if (MessageBox.Show("Không còn tồn tại VTPT trong Phiếu Xuất Kho.\n Bạn có muốn XÓA HẲN PHIẾU XUẤT KHO?", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
                                    {
                                        objPXH.TinhTrang = 1;
                                        objPXH.NgayXuat = DateTime.Today;
                                        objPXH.NguoiXuat = _nguoitao;

                                        clsPHIEU_XUAT_HANG pxhCtrl = new clsPHIEU_XUAT_HANG(_sConn);
                                        if (pxhCtrl.usp_UpdatePHIEU_XUAT_HANG(objPXH) > 0)
                                        {
                                            this.cmdThemVTPT.Enabled = false;
                                            this.cmdCapNhatSL.Enabled = false;
                                        }
                                    }
                                }
                            }
                            else
                                MessageBox.Show("XÓA KHÔNG THÀNH CÔNG");
                        }
                    }
                }
                else
                    MessageBox.Show("XÓA KHÔNG THÀNH CÔNG!\nPHIẾU XUẤT KHO ĐÃ ĐƯỢC DUYỆT");

            }
            catch { }
        }

        private void lookupKho_QueryPopUp(object sender, CancelEventArgs e)
        {
            try
            {
                this.InitKho(this.txtMaVTPT.Text);
            }
            catch { }

        }

        private void lookUpTrangthai_QueryPopUp(object sender, CancelEventArgs e)
        {
            try
            {
                string makho = this.lookUpKho.GetColumnValue("MaKho").ToString();
                this.InitTrangthai(this.txtMaVTPT.Text, makho);
            }
            catch { }
        }

        private void cmdThemVTPT_Click(object sender, EventArgs e)
        {

            try
            {
                int soluongxuat = 0;
                int soluongton = 0;
                string matrangthai = this.lookUpTrangthai.GetColumnValue("MaTrangThai").ToString();
                string makho = this.lookUpKho.GetColumnValue("MaKho").ToString();
                string mavtpt = this.txtMaVTPT.Text;

                soluongxuat = int.Parse(this.txtSoLuong.Text.Trim());
                soluongton = int.Parse(this.lblsoluongton.Text);
                if (soluongxuat <= soluongton && matrangthai != "-1" && soluongxuat > 0)
                {
                    CHI_TIET_PXH obj = new CHI_TIET_PXH();
                    clsCHI_TIET_PXH ctpxhCtrl = new clsCHI_TIET_PXH(_sConn);

                    int k = lstTonKho.FindIndex(delegate(CHI_TIET_PXH x) { return x.MaThietBi == mavtpt && x.MaTrangThai == int.Parse(matrangthai) && x.MaKho == makho; });
                    int kq = -1;
                    if (k >= 0)
                    {
                        if (soluongxuat + lstTonKho[k].SoLuong <= soluongton)
                        {
                            // cap nhat
                            obj = lstTonKho[k];
                            obj.SoLuong += soluongxuat;
                            obj.NgayNhap = DateTime.Today;
                            obj.NguoiNhap = _nguoitao;

                            kq = ctpxhCtrl.usp_UpdateCHI_TIET_PXH(obj);
                        }
                        else
                            MessageBox.Show("TỔNG SỐ LƯỢNG XUẤT CỦA VTPT TRONG MỘT LẦN XUẤT KHO PHẢI NHỎ HƠN HOẶC BẰNG SỐ LƯỢNG TỒN");
                    }
                    else
                    {
                        // them moi
                        obj.MaThietBi = mavtpt;
                        obj.SoLuong = soluongxuat;
                        obj.MaKho = makho;
                        obj.MaTrangThai = int.Parse(matrangthai);
                        obj.NgayNhap = DateTime.Today;
                        obj.NguoiNhap = _nguoitao;
                        obj.MaPhieuXuatHang = this.txtMaPhieuXuatHang.Text.Trim();
                        kq = ctpxhCtrl.InsertCHI_TIET_PXH(obj);
                       
                    }

                  

                    if (kq != -1)
                    {
                        this.lookUpTrangthai.ItemIndex = 0;
                        this.HienthiCHI_TIET_PXH(this.txtMaPhieuXuatHang.Text);
                    }
                    else
                        MessageBox.Show("THÊM MỚI KHÔNG THÀNH CÔNG");
                    
                }
                else
                    this.txtSoLuong.Text = "0";
            }
            catch { }
        }

        private void cmdCapNhatSL_Click(object sender, EventArgs e)
        {
            try
            {
                if (gridDSVT.FocusedRowHandle >= 0)
                {

                    int soluongxuat = int.Parse(this.txtSoLuong.Text.Trim());
                    int soluongton = int.Parse(this.lblsoluongton.Text);
                    string matrangthai = this.lookUpTrangthai.GetColumnValue("MaTrangThai").ToString();
                    string makho=this.lookUpKho.GetColumnValue("MaKho").ToString();

                    // neu thong tin sau cap nhat khong thay doi so voi truoc cap nhat
                    if (lstTonKho[gridDSVT.FocusedRowHandle].MaThietBi == this.txtMaVTPT.Text && lstTonKho[gridDSVT.FocusedRowHandle].MaKho == makho && lstTonKho[gridDSVT.FocusedRowHandle].MaTrangThai.ToString() == matrangthai && lstTonKho[gridDSVT.FocusedRowHandle].SoLuong == soluongxuat)
                        return;

                    // neu xuat kho tu PO thi ma trang thai phai giong nhau
                    if (lstTonKho[gridDSVT.FocusedRowHandle].MaThietBi == this.txtMaVTPT.Text && lstTonKho[gridDSVT.FocusedRowHandle].POThamChieuSAP != "" && lstTonKho[gridDSVT.FocusedRowHandle].MaTrangThai.ToString() != matrangthai)
                    {
                        MessageBox.Show("KHI XUẤT KHO THEO PO THÌ KHÔNG ĐƯỢC PHÉP THAY ĐỔI TRẠNG THÁI VTPT");
                        return;
                    }

                    if (soluongxuat <= soluongton && matrangthai != "-1" && lstTonKho[gridDSVT.FocusedRowHandle].MaThietBi == this.txtMaVTPT.Text)
                    {
                        CHI_TIET_PXH obj = new CHI_TIET_PXH();
                        clsCHI_TIET_PXH ctpxhCtrl = new clsCHI_TIET_PXH(_sConn);
                        obj = lstTonKho[gridDSVT.FocusedRowHandle];
                        obj.MaKho = makho;
                        obj.SoLuong = soluongxuat;
                        obj.NgayNhap = DateTime.Today;
                        obj.NguoiNhap = _nguoitao;
                        obj.MaTrangThai = int.Parse(this.lookUpTrangthai.GetColumnValue("MaTrangThai").ToString());

                        // nếu phiếu xuất kho từ PO thì phải kiểm tra số lượng của PO >= tổng số lượng xuất
                        if (lstTonKho[gridDSVT.FocusedRowHandle].POThamChieuSAP != "")
                        {
                            if (soluongxuat > lstTonKho[gridDSVT.FocusedRowHandle].SoLuong)
                            {
                                float slPXK = 0;
                                for (int i = 0; i < lstTonKho.Count; i++)
                                {
                                    if (lstTonKho[i].MaThietBi == this.txtMaVTPT.Text && lstTonKho[i].MaTrangThai.ToString() == matrangthai)
                                    {
                                        slPXK += lstTonKho[i].SoLuong;
                                    }
                                }
                                slPXK = slPXK - lstTonKho[gridDSVT.FocusedRowHandle].SoLuong + soluongxuat;
                                float slPO = 0;
                                clsCHI_TIET_PDH ctpdhCtrl = new clsCHI_TIET_PDH(_sConn);
                                List<CHI_TIET_PDH> lstCTPDH = new List<CHI_TIET_PDH>();
                                lstCTPDH = ctpdhCtrl.usp_SelectCHI_TIET_PDHByPOThamChieuSAP(lstTonKho[gridDSVT.FocusedRowHandle].POThamChieuSAP);

                                for (int i = 0; i < lstCTPDH.Count; i++)
                                {
                                    if (lstCTPDH[i].MaThietBi == this.txtMaVTPT.Text && lstCTPDH[i].MaTrangThai.ToString() == matrangthai)
                                    {
                                        slPO += lstCTPDH[i].SoLuong;
                                    }
                                }

                                if (slPXK > slPO)
                                {
                                    MessageBox.Show("CẬP NHẬT KHÔNG THÀNH CÔNG\nTỔNG SỐ LƯỢNG XUẤT KHO PHẢI NHỎ HƠN HOẶC BẰNG SỐ LƯỢNG PO");
                                }
                            }

                        }
                        if (ctpxhCtrl.usp_UpdateCHI_TIET_PXH(obj) > 0)
                        {

                            lstTonKho[gridDSVT.FocusedRowHandle].SoLuong = int.Parse(this.txtSoLuong.Text);
                            lstTonKho[gridDSVT.FocusedRowHandle].MaKho = this.lookUpKho.GetColumnValue("MaKho").ToString();
                            lstTonKho[gridDSVT.FocusedRowHandle].MaTrangThai = int.Parse(this.lookUpTrangthai.GetColumnValue("MaTrangThai").ToString());

                            this.gridControl1.RefreshDataSource();
                        }
                        else
                        {
                            MessageBox.Show("CẬP NHẬT KHÔNG THÀNH CÔNG");
                        }
                    }
                    else
                        MessageBox.Show("THÔNG TIN NHẬP VÀO KHÔNG HỢP LỆ");

                }
                else
                    MessageBox.Show("Vui lòng chọn dòng có VTPT cần thay đổi thông tin ");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void lnkPhieuChuaDuyet_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                CHI_TIET_PXH oCTPXH = new CHI_TIET_PXH();
                oCTPXH.MaThietBi = lstTonKho[gridDSVT.FocusedRowHandle].MaThietBi;
                oCTPXH.MaKho = lstTonKho[gridDSVT.FocusedRowHandle].MaKho;
                oCTPXH.MaTrangThai = lstTonKho[gridDSVT.FocusedRowHandle].MaTrangThai;

                frmTimPhieuXK f = new frmTimPhieuXK(_sConn, oCTPXH, 0);
                f.chonPXK += new frmTimPhieuXK.ChonPhieuXuatKho(GetPhieuXuatKho1);
                f.ShowDialog();
            }
            catch { }
        }

        private void GetPhieuXuatKho1(object sender, ChonPhieuXKEventArgs e)
        {
            string mapxk = e.MaPhieuXuatKho;

            frmHienthiPhieuXK f = new frmHienthiPhieuXK(_sConn, mapxk);
            f.ShowDialog();
        }

        private void lookUpTrangthai_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            try
            {
                string matrangthai = this.lookUpTrangthai.GetColumnValue("MaTrangThai").ToString();
                string makho = this.lookUpKho.GetColumnValue("MaKho").ToString();
                string mavtpt = this.txtMaVTPT.Text;
                if (makho != "" && matrangthai != "" && matrangthai!="-1")
                {
                    this.txtSoLuong.Enabled = true;
                    clsTON_KHO tonCtrl = new clsTON_KHO(_sConn);
                    List<TON_KHO> lstton = new List<TON_KHO>();
                    lstton = tonCtrl.usp_SelectTON_KHO_ByMaKho(makho, mavtpt, int.Parse(matrangthai), null, null);
                    float soluongmax = 0;
                    for (int i = 0; i < lstton.Count; i++)
                    {
                        soluongmax += lstton[i].SoLuong;
                    }


                    // kiem tra xem VTPT nay co cac phieu xuat kho nao chua duoc duyet
                    List<CHI_TIET_PXH> lstctpxh = new List<CHI_TIET_PXH>();
                    clsCHI_TIET_PXH ctpxhCtrl = new clsCHI_TIET_PXH(_sConn);
                    lstctpxh = ctpxhCtrl.usp_CheckSoLuongXuatKhoChuaDuyet(mavtpt, makho, int.Parse(matrangthai));
                    for (int i = 0; i < lstctpxh.Count; i++)
                    {
                        if (lstctpxh[i].MaPhieuXuatHang!=this.txtMaPhieuXuatHang.Text)
                            soluongmax -= lstctpxh[i].SoLuong;
                    }

                    // kiem tra xem VTPT nay da ton tai trong list can xuat kho chua
                    for (int i = 0; i < lstTonKho.Count; i++)
                    {
                        if (lstTonKho[i].MaThietBi == mavtpt && lstTonKho[i].MaKho == makho && lstTonKho[i].MaTrangThai == int.Parse(matrangthai))
                            soluongmax -= lstTonKho[i].SoLuong;
                    }

                    this.lblsoluongton.Text = soluongmax.ToString();
                }
                else
                {
                    this.txtSoLuong.Enabled = false;
                }
            }
            catch { }
        }

        private void lookUpKho_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            try
            {
                string matrangthai = this.lookUpTrangthai.GetColumnValue("MaTrangThai").ToString();
                string makho = this.lookUpKho.GetColumnValue("MaKho").ToString();
                string mavtpt = this.txtMaVTPT.Text;
                if (makho != "" && matrangthai != "" && matrangthai!="-1")
                {
                    this.txtSoLuong.Enabled = true;
                    clsTON_KHO tonCtrl = new clsTON_KHO(_sConn);
                    List<TON_KHO> lstton = new List<TON_KHO>();
                    lstton = tonCtrl.usp_SelectTON_KHO_ByMaKho(makho, mavtpt, int.Parse(matrangthai), null, null);
                    float soluongmax = 0;
                    for (int i = 0; i < lstton.Count; i++)
                    {
                        soluongmax += lstton[i].SoLuong;
                    }


                    // kiem tra xem VTPT nay co cac phieu xuat kho nao chua duoc duyet
                    List<CHI_TIET_PXH> lstctpxh = new List<CHI_TIET_PXH>();
                    clsCHI_TIET_PXH ctpxhCtrl = new clsCHI_TIET_PXH(_sConn);
                    lstctpxh = ctpxhCtrl.usp_CheckSoLuongXuatKhoChuaDuyet(mavtpt, makho, int.Parse(matrangthai));
                    for (int i = 0; i < lstctpxh.Count; i++)
                    {
                        if (lstctpxh[i].MaPhieuXuatHang != this.txtMaPhieuXuatHang.Text)
                            soluongmax -= lstctpxh[i].SoLuong;
                    }

                    // kiem tra xem VTPT nay da ton tai trong list can xuat kho chua
                    for (int i = 0; i < lstTonKho.Count; i++)
                    {
                        if (lstTonKho[i].MaThietBi == mavtpt && lstTonKho[i].MaKho == makho && lstTonKho[i].MaTrangThai == int.Parse(matrangthai))
                            soluongmax -= lstTonKho[i].SoLuong;
                    }

                    this.lblsoluongton.Text = soluongmax.ToString();
                }
                else
                {
                    this.txtSoLuong.Enabled = false;
                }
            }
            catch { }
        }

        private void tìmTheoNhiềuTiêuChíToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frm == null)
            {
                frm = new frmTimKiemVTPT(_sConn,true);
                frm.SelectLstMaterial += new frmTimKiemVTPT.SelectListMaterialHandler(LuuDSVTPT);
            }
            else
                lstVTPT.Clear();

            frm.ShowDialog();
            if (lstVTPT.Count > 0)
            {
                if (lstVTPT.Count > 1)
                {

                    MessageBox.Show("BẠN ĐANG CHỌN " + lstVTPT.Count.ToString() + " ĐỂ XUẤT KHO\nVUI LÒNG NHẬP THÔNG TIN CHO TỪNG VTPT ĐỂ XUẤT KHO");
                }
               
            }
        }

        private void tìmTheoHìnhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frm1 == null)
            {
                frm1 = new frmTimKiemTheoHinh(_sConn,true);
                frm1.SelectLstMaterialByImg += new frmTimKiemTheoHinh.SelectListMaterialByImgHandler(LuuDSVTPT);
            }
            else
                lstVTPT.Clear();
            frm1.ShowDialog();
            if (lstVTPT.Count > 0)
            {
                if (lstVTPT.Count > 1)
                {

                    MessageBox.Show("BẠN ĐANG CHỌN " + lstVTPT.Count.ToString() + " ĐỂ XUẤT KHO\nVUI LÒNG NHẬP THÔNG TIN CHO TỪNG VTPT ĐỂ XUẤT KHO");
                }
               
            }
        }

        private void LuuDSVTPT(object sender, lstVtptEventArgs e)
        {
            lstVTPT = e.lstVtpt;
            if (lstVTPT.Count > 0)
            {
                this.txtMaVTPT.Text = lstVTPT[0]._maVTPT;
                this.lblTenVTPT.Text = lstVTPT[0]._tenVTPT;
                this.lookUpTrangthai.Enabled = true;
            }
        }

        private void txtMaVTPT_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                // kiểm tra tồn tại của VTPT                   

                VAT_TU_PHU_TUNG obj = new VAT_TU_PHU_TUNG();

                clsVAT_TU_PHU_TUNG vtptCtrl = new clsVAT_TU_PHU_TUNG(_sConn);

                obj = vtptCtrl.SelectVAT_TU_PHU_TUNG(this.txtMaVTPT.Text.Trim(), frmDangNhap.objTau.maTau);
               
                    if (obj != null && obj._maVTPT != "")
                    {
                        this.lblTenVTPT.Text = obj._tenVTPT;
                        if (this.txtNgayDuyet.Text == "")
                        {                           
                            this.cmdCapNhatSL.Enabled = true;
                            this.cmdThemVTPT.Enabled = true;
                        }
                    }
                    else
                    {
                        this.cmdCapNhatSL.Enabled = false;
                        this.cmdThemVTPT.Enabled = false;
                        MessageBox.Show("KHÔNG TÌM THẤY THÔNG TIN VẬT TƯ NÀY");
                    }
                
            }
        }

        private void tìmVTIMPAThToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frm2 == null)
            {
                frm2 = new frmImpa_TimKiem(_sConn,true);
                frm2.SelectLstMaterial += new frmImpa_TimKiem.SelectListMaterialHandler(LuuDSVTPT);
            }
            else
                lstVTPT.Clear();
            frm2.ShowDialog();
            if (lstVTPT.Count > 0)
            {
                if (lstVTPT.Count > 1)
                {

                    MessageBox.Show("BẠN ĐANG CHỌN " + lstVTPT.Count.ToString() + " ĐỂ XUẤT KHO\nVUI LÒNG NHẬP THÔNG TIN CHO TỪNG VTPT ĐỂ XUẤT KHO");
                }

            }
        }

        private void oeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frm3 == null)
            {
                frm3 = new frmImpa_TimKiemTheoHinh(_sConn,true);
                frm3.SelectLstMaterial += new frmImpa_TimKiemTheoHinh.SelectListMaterialHandler(LuuDSVTPT);
            }
            else
                lstVTPT.Clear();
            frm3.ShowDialog();
            if (lstVTPT.Count > 0)
            {
                if (lstVTPT.Count > 1)
                {

                    MessageBox.Show("BẠN ĐANG CHỌN " + lstVTPT.Count.ToString() + " ĐỂ XUẤT KHO\nVUI LÒNG NHẬP THÔNG TIN CHO TỪNG VTPT ĐỂ XUẤT KHO");
                }

            }
        }

        private void frmChinhsuaPhieuXK_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.I)
            {
                this.cmdThemVTPT_Click(null, null);
            }
            else
                if (e.Control && e.KeyCode == Keys.U)
                {
                    this.cmdCapNhatSL_Click(null, null);
                }
        }

       
    }
}